package com.kuaimacode.kframework.admin.controller.sysparam;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.sysparam.SysParamService;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.mapper.sysparam.SysParamMapper;
import com.kuaimacode.kframework.mybatis.models.sysparam.SysParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.Subject;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author anzy
 * @date 2019/05/17
 */
/**
 * Controller
 */
@Controller
@RequestMapping({"/sysParam"})
public class SysParamController {
	private static String PAGE_FILE_NAME = "sysParam/";

	@Autowired
	private SysParamService sysParamServices;


	@RequestMapping(value = {"/index"})
	public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
										@RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
						ModelMap model) {
		PageInfo<SysParam> pageInfo = sysParamServices.findAll(pageNum, pageSize);
		// pageInfo.list sysParam
		model.put("pageInfo", pageInfo);
		return PAGE_FILE_NAME + "index";
	}


	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap map) {
		SysParam sysParam = new SysParam();
		map.put("sysParam", sysParam);
		map.put("isAdd", true);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable String id, ModelMap map) {
		SysParam sysParam = sysParamServices.getById(id);
		String[] values = sysParam.getValue().split(",");
		map.put("sysParam", sysParam);
        map.put("values", values);
		map.put("isAdd", false);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
	@ResponseBody
	public JsonResult edit(SysParam sysParam, Boolean isAdd,ModelMap map) {
		try {
		    String valueStr = Optional.ofNullable(sysParam.getValue())
                    .map(value -> value.replaceAll("，", ","))
                    .orElse("");
            sysParam.setValue(Arrays.stream(valueStr.split(","))
                    .filter(StringUtils::isNotBlank)
                    .collect(Collectors.joining(",")));
			if(isAdd){
				// 新建保存
				sysParamServices.save(sysParam);
			}else{
				// 编辑保存
				sysParamServices.updateById(sysParam);
			}
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	public String detail(@PathVariable String id, ModelMap map) {
		SysParam sysParam = sysParamServices.getById(id);
		map.put("sysParam", sysParam);
		return PAGE_FILE_NAME+"detail";
	}


	@ResponseBody
	public JsonResult delete(@PathVariable String id, ModelMap map) {
		try {
			sysParamServices.removeById(id);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


}
